package com.kaiwukj.android.ufamily.mvp.ui.page.account.login.forget;

import android.os.Bundle;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerAccountComponent;
import com.kaiwukj.android.ufamily.di.module.AccountModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.ForgetPassEvent;
import com.kaiwukj.android.ufamily.mvp.ui.page.account.AccountContract;
import com.kaiwukj.android.ufamily.mvp.ui.page.account.AccountPresenter;
import com.kaiwukj.android.ufamily.mvp.ui.page.account.login.SendVerifyCodeFragment;

import org.greenrobot.eventbus.Subscribe;

import butterknife.OnClick;
import me.yokeyword.fragmentation.ISupportFragment;

@Route(path = ARouterUrlKt.ForgetPassUrl)
public class ForgetPassActivity extends BaseMvpActivity<AccountPresenter> implements AccountContract.View {

    private SendVerifyCodeFragment sendVerifyCodeFragment;

    private String mobile;

    @Autowired
    int way = 0;//normal:0  wechat:1
    @Autowired
    String extra;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerAccountComponent.builder()
                .appComponent(appComponent)
                .accountModule(new AccountModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_base_content;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        loadRootFragment(R.id.fl_content,
                (ISupportFragment) ARouter.getInstance()
                        .build(ARouterUrlKt.ForgetPassFragmentUrl)
                        .withInt("pos", 0)
                        .withInt("type", way)
                        .navigation());
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }


    @Subscribe
    public void onForgetPassEvent(ForgetPassEvent event) {
        if (event.getCode() == 1) {//验证码页面
            mobile = (String) event.getObj();
            mPresenter.sendVerifyCode(mobile);
            start(sendVerifyCodeFragment = SendVerifyCodeFragment.newInstance(mobile));
            sendVerifyCodeFragment.setOnVerifyCodeSendListener(new SendVerifyCodeFragment.OnVerifyCodeSendListener() {
                @Override
                public void onVerify(String mobile, String code) {
                    if (way == 0) {
                        mPresenter.loginByCode(mobile, code);
                    } else if (way == 1) {
                        mPresenter.loginByWechat(extra, mobile, code);
                    }
                }

                @Override
                public void onReply(String mobile) {
                    // showMessage("重新获取验证码");
                    mPresenter.sendVerifyCode(mobile);

                }
            });
        } else if (event.getCode() == 2) {
            String pass = (String) event.getObj();
            //showMessage("新密码是:" + pass);
            mPresenter.updatePass(pass);
        }
    }

    @Override
    public void loginSuccess() {
        if (way == 0) {
            start((ISupportFragment) ARouter.getInstance()
                    .build(ARouterUrlKt.ForgetPassFragmentUrl)
                    .withInt("pos", 1)
                    .navigation());
        } else {
            if (getApp().getUserInfo().getLastCommunityId() == null || getApp().getUserInfo().getLastCommunityId() < 0) {
                ARouter.getInstance().build(ARouterUrlKt.CommunityUpdateUrl).navigation();
            } else {
                ARouter.getInstance().build(ARouterUrlKt.MainRouterUrl).navigation();
            }
        }
    }

    @Override
    public void wechatBindMobild(String openId) {

    }


    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        switch (taskId) {
            case AccountPresenter.PASS_UPDATE:
                showMessage("密码修改成功,请重新登录");
                finish();
                break;
            case AccountPresenter.VERIFY_CODE_SEND:
                showMessage("发送验证码成功!");
                break;
        }
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
    }
}
